package com.example.recordlib.utli;

import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;

import android.content.Context;
import android.os.Environment;

import com.arthenica.mobileffmpeg.ExecuteCallback;
import com.arthenica.mobileffmpeg.FFmpeg;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author xtxiaolu
 * @description: 录音文件转换器
 * @date :2023/6/6
 */
public class AudioConverter {
    private Context mContext;
    private File audioFile;
    private AudioFormat format;
    private ConversionCallback callback;

    private AudioConverter(Context context) {
        this.mContext = context;
    }

    public static AudioConverter with(Context context) {
        return new AudioConverter(context);
    }

    public AudioConverter setFile(File originalFile) {
        this.audioFile = originalFile;
        return this;
    }

    public AudioConverter setFormat(AudioFormat format) {
        this.format = format;
        return this;
    }

    public AudioConverter setCallback(ConversionCallback callback) {
        this.callback = callback;
        return this;
    }

    public void convert() {
        if (audioFile == null || format == null || callback == null) {
            throw new IllegalArgumentException("Incomplete conversion parameters.");
        }

        if (!audioFile.exists()) {
            callback.onConversionFailed(new FileNotFoundException("Audio file not found."));
            return;
        }

        File outputDirectory = mContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
        File outputFile = new File(outputDirectory, "output.mp3");
        String[] command = getConversionCommand(audioFile.getAbsolutePath(), outputFile.getAbsolutePath(), format);

        try {
            FFmpeg.executeAsync(command, new ExecuteCallback() {
                @Override
                public void apply(final long executionId, final int returnCode) {
                    if (returnCode == RETURN_CODE_SUCCESS) {
                        callback.onConversionCompleted(outputFile.getAbsolutePath());
                    } else {
                        callback.onConversionFailed(new Exception("Conversion failed. Return code: " + returnCode));
                    }
                }
            });
        } catch (Exception e) {
            callback.onConversionFailed(e);
        }
    }

    private String[] getConversionCommand(String inputPath, String outputPath, AudioFormat format) {
        String audioCodec = format == AudioFormat.MP3 ? "libmp3lame" : "libfaac";
        String[] command = {
                "-y",
                "-i",
                inputPath,
                "-c:a",
                audioCodec,
                outputPath
        };
        return command;
    }

    public enum AudioFormat {
        AMR,
        MP3
    }

    public interface ConversionCallback {
        void onConversionCompleted(String outputPath);
        void onConversionFailed(Exception exception);
    }
}
